package com.uzhie.web.controller.http;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.uzhie.exception.EquipmentException;
import com.uzhie.web.anno.Auth;
import com.uzhie.web.bean.PageBean;
import com.uzhie.web.bean.PayloadBean;
import com.uzhie.web.controller.http.comm.CommController;
import com.uzhie.web.module.Equipment;
import com.uzhie.web.service.EquipmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by Zhangdequan on 2018/7/18.
 */
@RestController
@RequestMapping(value = "equipment")
public class EquipmentController extends CommController {

    @Autowired
    private EquipmentService equipmentService;

    /**
     * 根据条件查询设备信息
     *
     * @param body
     * @param page
     * @return
     * @throws IllegalAccessException
     * @throws InstantiationException
     */
    @Auth(tokenRequired = false)
    @RequestMapping(value = "list")
    public PayloadBean selectVehicleByCondition(@RequestBody(required = false) JSONObject body, @RequestBody(required = false) PageBean page) {
        List<Equipment> list = null;
        try {
            String userId = body.getString("userId");
            list = equipmentService.selectAllByCondition(userId,
                    body.getString("deviceNumber"),
                    body.getString("simNumber"),
                    body.getString("mfrsName"),
                    body.getString("deviceType"),
                    body.getString("minCreateTime"),
                    body.getString("maxCreateTime"),
                    body.getString("departName"),
                    page

            );
            return buildResponseWithPage(list, page);
        } catch (IllegalArgumentException e) {
            return buildResponseWithPage(null, page, setError(0, e.getMessage()));
        }
    }

    /**
     * 添加设备信息
     *
     * @param body
     * @return
     * @throws IllegalAccessException
     * @throws InstantiationException
     */
    @Auth
    @RequestMapping(value = "addEquipment")
    public PayloadBean addEquipment(@RequestBody(required = false) JSONObject body) {
        try {
            equipmentService.addEquipment(JSON.toJavaObject(body, Equipment.class));
            return buildResponse(null);
        } catch (EquipmentException e) {
            return buildResponse(null, setError(0, e.getMessage()));
        }
    }


    /**
     * 根据id,查询设备信息详情
     *
     * @param body
     * @return
     * @throws IllegalAccessException
     * @throws InstantiationException
     */
    @Auth
    @RequestMapping(value = "selectEquipmentById")
    public PayloadBean selectEquipmentById(@RequestBody(required = false) JSONObject body) {
        Equipment equipment;
        try {
            equipment = equipmentService.selectEquipmentById(body.getString("equipmentId"));
            return buildResponse(equipment);
        } catch (IllegalArgumentException e) {
            return buildResponse(null, setError(0, e.getMessage()));
        }
    }

    /**
     * 更新设备信息
     *
     * @param body
     * @return
     * @throws IllegalAccessException
     * @throws InstantiationException
     */
    @Auth
    @RequestMapping(value = "updateEquipment")
    public PayloadBean updateEquipment(@RequestBody(required = false) JSONObject body) {
        try {
            equipmentService.updateEquipment(JSON.toJavaObject(body, Equipment.class));
            return buildResponse(null);
        } catch (EquipmentException e) {
            return buildResponse(null, setError(0, e.getMessage()));
        }
    }


    /**
     * 根据设备id,批量更新设备状态信息
     *
     * @param body
     * @return
     * @throws IllegalAccessException
     * @throws InstantiationException
     */
    @Auth
    @RequestMapping(value = "updateStatus")
    public PayloadBean updateEquipmentStatusById(@RequestBody(required = false) JSONObject body) {
        try {
            int status = body.getInteger("status");
            List<Equipment> equipments = JSONArray.parseArray(body.getString("equipmentIds"), Equipment.class);
            equipmentService.updateEquipmentStatusById(equipments, status);
            return buildResponse(null);
        } catch (EquipmentException e) {
            return buildResponse(null, setError(0, e.getMessage()));
        }
    }


    /**
     * 设备绑定车辆信息
     *
     * @param body
     * @return
     * @throws IllegalAccessException
     * @throws InstantiationException
     */
    @Auth
    @RequestMapping(value = "bindVehicles")
    public PayloadBean bindVehicles(@RequestBody(required = false) JSONObject body) {
        try {
            equipmentService.bindVehicles(body.getString("equipmentId"), body.getString("vehicleId"));
            return buildResponse(null);
        } catch (EquipmentException e) {
            return buildResponse(null, setError(0, e.getMessage()));
        }
    }

    /**
     * 设备解绑车辆
     *
     * @param body
     * @return
     * @throws IllegalAccessException
     * @throws InstantiationException
     */
    @Auth
    @RequestMapping(value = "unbindVehicles")
    public PayloadBean unbindVehicles(@RequestBody(required = false) JSONObject body) {
        PayloadBean payloadBean = null;
        try {
            equipmentService.unbindVehicles(body.getString("equipmentId"));
            return buildResponse(null);
        } catch (EquipmentException e) {
            return buildResponse(null, setError(0, e.getMessage()));
        }
    }

}
